﻿// SMIL is a community project avaipublic lable under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// View the project and the latest code at http://smil.codeplex.com/

using System;
using System.Windows;
using System.Windows.Media;
using System.Collections.Generic;
using Microsoft.Maps.MapControl.Core;

using GeoAPI.Geometries;
using DeepEarth.BingMapsToolkit.Client.MapGeometry;
using DeepEarth.BingMapsToolkit.Client.Common.Entities;
 
using Location = Microsoft.Maps.MapControl.Location;

namespace SMIL.DE.Extensions
{
    public static class Extensions
    {

        public static SelectGeometry ToSelectGeometry(this IGeometry geometry, StyleSpecification style,  MapCore parentMap, EnhancedMapLayer layer)
        {
            return new SelectGeometry(geometry, style.Clone(), parentMap, layer);
        }


        public static List<T> GetChildObjects<T>(this DependencyObject obj, string name = null)
        {
            var retVal = new List<T>();
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                object c = VisualTreeHelper.GetChild(obj, i);
                if (c.GetType().FullName == typeof(T).FullName && (String.IsNullOrEmpty(name) || ((FrameworkElement)c).Name == name))
                {
                    retVal.Add((T)c);
                }
                var gc = ((DependencyObject)c).GetChildObjects<T>(name);
                if (gc != null)
                    retVal.AddRange(gc);
            }

            return retVal;
        }

        public static T GetChildObject<T>(this DependencyObject obj, string name = null) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                object c = VisualTreeHelper.GetChild(obj, i);
                if (c.GetType().FullName == typeof(T).FullName && (String.IsNullOrEmpty(name) || ((FrameworkElement)c).Name == name))
                {
                    return (T)c;
                }
                object gc = ((DependencyObject)c).GetChildObject<T>(name);
                if (gc != null)
                    return (T)gc;
            }

            return null;
        }

    }
}
